import collections
import numpy as np
import sys, os
import timeit

# 在一个文本中找出重复的关键词
# 关键词的最小长度为a
# 输出是最小同时出现的字词

def main2(a):

  with open(a, encoding='utf8') as f:
    v = f.read()
  v2 = np.asarray(list(map(ord, v)), dtype=int)

  v3 = np.nditer(v2, flags=['f_index'])
  # v4  ord字符队列v2的迭代器

  v_index = collections.defaultdict(lambda: [])
  '''
  v9_dict = collections.defaultdict(lambda: np.empty((0,), dtype= int))
  v23 = np.empty((0,), dtype= int)
  v25_list = []
  v11_size = v2.size
  v22 = np.asarray((ord('\n'), ord('、'), ord('.'), ord('%'), ord('①'), ord('②'), ord('—'), ord('。'), ord('：'), ord('；'), ord('，'), ord('('), ord(')'), ord('（'), ord('）'), ord('+')))
  isError = False
  '''

  while v3.index <= v2.size -2:

    v4= np.where(v2== v3[0])[0] +1
    v5= np.where(v2== v2[v3.index+1])[0]
    v6= np.concatenate((v4, v5), axis=0)
    # v7  重合位置的个数
    v7= v6.size - np.unique(v6).size
    #print(v7)
    if v7 >=2 and v3[0]!=ord('\n') and v2[v3.index+1]!=ord('\n'):
      v_index[v7].append((v3.index, v3.index+1))

    v3.iternext()

  for i1 in v_index:
    v_index[i1]= np.sort(np.unique(v_index[i1]))

  v8= np.sort(np.unique(np.append(np.array(np.concatenate(list(v_index.values()), axis=0)), -1)))
  v9 = np.where(np.diff(v8) != 1)[0] + 1
  v10= []
  for i2 in v8[v9] - v8[v9-1]:
    v10.append((i2-1)*'\u25A1')
    #print(i2, v8[v9] - v8[v9-1])
  
  v11= np.split(v8, v9)
  v12= []
  for i3 in v11:
    #print(i3, v11, v8,v9, v_index)
    v12.append(''.join(list(map(chr, v2[i3].tolist()))))

  v_text = ''.join([sub[item] for item in range(len(v10)) for sub in [v12, v10]])

  v_enter = np.sort(np.where(v2== ord('\n')))
  v_list_text = list(v_text)
  for i4 in np.nditer(v_enter):
    try:
      v_list_text[i4+1] = '\n'
    except:
      v_list_text.append('\n')
  v_text = ''.join(v_list_text)

  print("\n\t共%i字" %v2.size)
  return v_index, v_text


def main(a):

  v_start = timeit.default_timer()
  for i in a:
    execout, _ = main2(i)
    v2= execout[max(execout.keys())]
    v3= np.where(np.diff(v2) != 1)[0] + 1
    v4= np.size(v2)
    v5= np.sort(np.append(np.concatenate([v3-1, v3]), [0, np.size(v2)]))
    for 
    print(v2,v3,v4, v5)

  print("\nTime It > %s Sec" %(timeit.default_timer()-v_start))


if __name__ == "__main__":
  if len(sys.argv)>=2:
    main(sys.argv[1:])
  else:
    print()
